//
//  SSSRecommendRecordController.m
//  SSSMerchant
//
//  Created by Darker on 2017/3/23.
//  Copyright © 2017年 xnxin. All rights reserved.
//

#import "SSSRecommendRecordController.h"
#import "SSSRecommendRecordCell.h"
#import "SSSRecommendModel.h"

@interface SSSRecommendRecordController ()<UITableViewDelegate,UITableViewDataSource>

{
    
    UITableView *_bgTableView;
    NSMutableArray *_dataArray;
    NSInteger _currentPage;
}
/** 请求参数 */
@property(nonatomic, strong) NSMutableDictionary *parameter;

@end

static NSString *RecommendRecordID    = @"RecommendRecordID";

@implementation SSSRecommendRecordController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.title = @"推荐记录";
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTabelView];
    
    [self reloadData];
}
- (void)initTabelView{
    
    _bgTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, KSCREEN_Width, KSCREEN_Height) style:UITableViewStylePlain];
    [self.view addSubview:_bgTableView];
    _bgTableView.delegate = self;
    _bgTableView.dataSource = self;
    _bgTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _bgTableView.backgroundColor = [UIColor colorWithHex:@"FFFFFF"];
    _bgTableView.rowHeight = HeigthIos6CompeteWithCurrent(74);
    //_bgTableView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
    //return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Class currentClass = [SSSRecommendRecordCell class];
    
    SSSRecommendRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(currentClass)];
    if (!cell) {
        cell = [[SSSRecommendRecordCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:RecommendRecordID];
    }
    cell.model = _dataArray[indexPath.row];
    return cell;
    
}
- (void)reloadData{
    
    ZDRefreshHeader *header = [ZDRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 马上进入刷新状态
    [header beginRefreshing];
    // 设置header
    _bgTableView.mj_header = header;
    
    // 上拉加载
    _bgTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];

    
}
- (void)loadNewData{
    
    [_bgTableView.mj_footer endRefreshing];
    _currentPage = 1;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"currentPage"] = @(_currentPage);
    params[@"pageSize"] = @"20";
    self.parameter = params;
    
    [[XHRequestManager manager] getRequest:kConfigPath(kRecommendRecrod) params:params success:^(id  _Nonnull responseObj) {
        
        XHLog(@"-----%@",responseObj);
        if ([responseObj[@"status"] intValue] == 0){
            
            if ([responseObj[@"data"][@"pageList"] count] == 0) {
                
                showTipHudStr(@"暂无数据");
                return ;
            }
            [_dataArray removeAllObjects];
            NSArray *array = [SSSRecommendModel mj_objectArrayWithKeyValuesArray:responseObj[@"data"][@"pageList"]];
            _dataArray = [NSMutableArray array];
            [_dataArray addObjectsFromArray:array];
            
            //不是最后一次请求
            if (self.parameter != params) return ;
            //回到主线程刷新表格
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //刷新表格
                [_bgTableView reloadData];
                
            });
            
            [_bgTableView.mj_header endRefreshing];
            
            //_listTableView.mj_footer.hidden = (_currentPage == 1);
            
            if ([responseObj[@"data"][@"hasNextPage"] intValue] == 0) {
                
                [_bgTableView.mj_footer endRefreshingWithNoMoreData];
                
            }else{
                
                [_bgTableView.mj_header endRefreshing];
            }
            
            
        }else{
            
            showTipHudStr(kResponseMessage);
            [_bgTableView.mj_header endRefreshing];
        }
        
        
    } failure:^(NSError * _Nonnull error) {
        
        showTipHudStr(error.localizedDescription);
        [_bgTableView.mj_header endRefreshing];
        
    }];

    
}
- (void)loadMoreData{
    
    [_bgTableView.mj_header endRefreshing];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"currentPage"] = @(++_currentPage);
    params[@"pageSize"] = @"20";
    self.parameter = params;
    
    [[XHRequestManager manager] getRequest:kConfigPath(kRecommendRecrod) params:params success:^(id  _Nonnull responseObj) {
        
        XHLog(@"-----%@",responseObj);
        if ([responseObj[@"status"] intValue] == 0){
            
            NSArray *array = [SSSRecommendModel mj_objectArrayWithKeyValuesArray:responseObj[@"data"][@"pageList"]];
            [_dataArray addObjectsFromArray:array];
            
            //不是最后一次请求
            if (self.parameter != params) return ;
            //回到主线程刷新表格
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //刷新表格
                [_bgTableView reloadData];
                
            });
            
            [_bgTableView.mj_header endRefreshing];
            
            //_listTableView.mj_footer.hidden = (_currentPage == 1);
            
            if ([responseObj[@"data"][@"hasNextPage"] intValue] == 0) {
                
                [_bgTableView.mj_footer endRefreshingWithNoMoreData];
                
            }else{
                
                [_bgTableView.mj_header endRefreshing];
            }
            
            
        }else{
            
            showTipHudStr(kResponseMessage);
            [_bgTableView.mj_header endRefreshing];
        }
        
        
    } failure:^(NSError * _Nonnull error) {
        
        showTipHudStr(error.localizedDescription);
        [_bgTableView.mj_header endRefreshing];
        
    }];

}
@end
